package com.fastloan.utils.excelutil;

import com.fastloan.utils.excelutil.model.BetweenDate;
import com.fastloan.utils.excelutil.model.ExpertExcelParam;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.fastloan.utils.DateTimeUtil.NORM_DATETIME_PATTERN_L;


/**
 * 导出excel时间处理工具
 *
 * @author yanwe
 *         createTime 2017-04-2017/4/5 15:26
 */
public class ExcelDateUtil {
    
    /**
     * 日期处理
     *
     * @param dateStr 日期字符串
     * @return 处理后的日期
     */
    public static LocalDateTime str2LocaldateTime(String dateStr) {
        LocalDateTime date = null;
        try {
            date =  LocalDateTime.parse(dateStr,NORM_DATETIME_PATTERN_L);
        } catch (Exception e) {
            try {
                LocalDate localDate = LocalDate.parse(dateStr, DateTimeFormatter.ISO_LOCAL_DATE);
                date = LocalDateTime.of(localDate, LocalTime.of(0, 0));
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
        return date;
    }

    /**
     * 根据传递的日期返回本周一至周日
     *
     * @param date 日期
     * @return 周一至周五的日期数组
     */
    public static LocalDateTime[] getWeekDay(LocalDateTime date) {
        Objects.requireNonNull(date);
        while (date.getDayOfWeek() != DayOfWeek.MONDAY) {
            date = date.plusDays(-1);
        }
        int dayNum = 7;
        LocalDateTime[] dates = new LocalDateTime[dayNum];
        for (int i = 0; i < dayNum; i++) {
            dates[i] = date;
            date = date.plusDays(1);
        }
        return dates;
    }

    public static List<BetweenDate> getMonthDay(ExpertExcelParam excelParam){
        Objects.requireNonNull(excelParam);
        LocalDateTime date = str2LocaldateTime(excelParam.getDate());
        List<BetweenDate> dateList = new ArrayList<>();
        //当前月份
        if(excelParam.getStep() == 0){
            BetweenDate betweenDate = getBetweenMonthDate(date);
            dateList.add(betweenDate);
            return dateList;
        }
        //是否只对应step月份
        if(excelParam.isOnly()){
            date = date.plusMonths(excelParam.getStep());
            BetweenDate betweenDate = getBetweenMonthDate(date);
            dateList.add(betweenDate);
            return dateList;
        }
        //连续月份
        //添加当前月
        dateList.add(getBetweenMonthDate(date));
        for (int i = 0; i < Math.abs(excelParam.getStep()); i++) {
            date = date.plusMonths(excelParam.getStep() > 0 ? 1 : -1);
            BetweenDate betweenDate = getBetweenMonthDate(date);
            dateList.add(betweenDate);
        }
        return dateList;
    }

    private static BetweenDate getBetweenMonthDate(LocalDateTime date) {
        BetweenDate betweenDate = new BetweenDate();
        betweenDate.setBeginDate(date.with(TemporalAdjusters.firstDayOfMonth()));
        betweenDate.setEndDate(date.with(TemporalAdjusters.lastDayOfMonth()));
        return betweenDate;
    }
}
